package com.mall4j.cloud.base.model;

import lombok.Getter;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Getter
public class BaseResult<T> {
    private final int code;
    private final String message;
    private final T data;
    private final Map<String, Object> additionalInfo;

    private BaseResult(int code, String message, T data, Map<String, Object> additionalInfo) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.additionalInfo = additionalInfo != null ? additionalInfo : Collections.emptyMap();
    }

    public static <T> BaseResult<T> success(T data) {
        return new BaseResult<>(200, "Success", data, null);
    }

    public static <T> BaseResult<T> error(int code, String message) {
        return new BaseResult<>(code, message, null, null);
    }

    public static <T> BaseResult<T> error(int code, String message, Throwable throwable) {
        Map<String, Object> errorInfo = new HashMap<>();
        errorInfo.put("exception", throwable.getMessage());
        return new BaseResult<>(code, message, null, errorInfo);
    }

}


